import { Node } from "cc";
import PoolHelper from "../helper/PoolHelper";

export default class Tools {
    /**
     * 遍历容器子对象，刷新数据，数据长度大于子对象时，补充子对象，多则回收
     * @param len 数据长度
     * @param container 容器
     * @param name 对象池key
     * @param callback 回调
     */
    public static flushItemList(len:number,container:Node,name:string,callback:(item: Node,i:number)=>void):void{
        const pool=PoolHelper.getInstance()
        const childrens=container.children
        let i=0,item:Node
        for(; i < len; i++){
            if(i<childrens.length){
                item=childrens[i]
            }else{
                item=pool.getNode(name)
                item.parent=container
            }
            callback(item,i)
        }
        if(i<childrens.length){
            while(childrens.length>i){
                item=childrens[childrens.length-1]
                item.parent=null
                pool.putNode(name,item)
            }
            /*const l=childrens.length-1
            for(i=l;i<l;i++){
                item=childrens[i]
                item.parent=null
                pool.putNode(name,item)
            }*/
        }
    }
}